package com.cn.wanxi.controller;

import com.alibaba.fastjson.JSON;
import com.cn.wanxi.entity.MailEntity;
import com.cn.wanxi.entity.UserEntity;
import com.cn.wanxi.rabbitmq.Receiver;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

@Controller
public class ElasticSearchController {
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @RequestMapping("/searchES")
    @ResponseBody
    public void searchES(MailEntity mailEntity) throws Exception {
        termQuery(mailEntity);
    }

    @RequestMapping("/searchMQ")
    @ResponseBody
    public void searchMQ(MailEntity mailEntity) throws Exception {
        new Receiver().process();
    }

    /**
     * 精确查询（查询条件不会进行分词，但是查询内容可能会分词，导致查询不到）
     */
    public void termQuery(MailEntity mailEntity) {
        try {
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//            searchSourceBuilder.query(QueryBuilders.fuzzyQuery("subject", mailEntity.getSubject()).fuzziness(Fuzziness.AUTO));
            // 创建查询请求对象，将查询对象配置到其中
            SearchRequest searchRequest = new SearchRequest("mail");

            SearchSourceBuilder sourceBuilder = searchSourceBuilder.query(QueryBuilders.termQuery("subject", mailEntity.getSubject()));
            searchRequest.source(sourceBuilder);
            // 执行查询，然后处理响应结果
            SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            // 根据状态和数据条数验证是否返回了数据
            if (RestStatus.OK.equals(searchResponse.status()) && searchResponse.getHits().getTotalHits().value > 0) {
                SearchHits hits = searchResponse.getHits();
                for (SearchHit hit : hits) {
                    // 将 JSON 转换成对象
                    UserEntity userInfo = JSON.parseObject(hit.getSourceAsString(), UserEntity.class);
                    // 输出查询信息
                    System.out.println(userInfo.toString());
                }
            }
        } catch (IOException e) {
            System.out.println("error");
        }
    }

//    public int getEsCount() throws IOException {
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        SearchSourceBuilder sourceBuilder = searchSourceBuilder.query(QueryBuilders.matchAllQuery());
//        SearchRequest searchRequest = new SearchRequest("");
//        searchRequest.source(sourceBuilder);
//        // 执行查询，然后处理响应结果
//        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//    }
}
